<template>
  <div class="incentive">
    <dt-search
      :xl="5"
      :xlBtn="9"
      v-model="searchInfo"
      :items="searchOpt"
      @search="onSearch"
      :labelWidth="labelWidth"
      @reset="onReset">
      <Button type="primary" @click="createIncentive" :disabled="disFlag" v-if="unlocking('0112020100')">
        新建在销激励
      </Button>
      <Button type="primary" v-if="needExtend" @click="onDefer">申请延期发起</Button>
      <Button type="primary">
        <a
          href="https://images.tospurfang.com/doc/%E5%9C%A8%E9%94%80%E6%BF%80%E5%8A%B1%E5%A5%96%E9%87%91%E6%93%8D%E4%BD%9C%E6%8C%87%E5%BC%95.pdf"
          style="color: #fff"
          target="_blank">
          查看操作指引
        </a>
      </Button>
    </dt-search>
    <create-incentive-modal ref="createIncentive" @success="success"></create-incentive-modal>
    <info-modal ref="infoModal"></info-modal>
    <dt-grid ref="table" :columns="columns" :list="list" />
    <page
      :pageData="pageData"
      @pageOnChangeFun="pageOnChangeFun"
      @pageOnPageSizeChangeFun="pageOnPageSizeChangeFun"></page>
    <defer-modal ref="defer" />
  </div>
</template>
<script>
  import columnOpt from './opt/column.js'
  import searchOpt from './opt/search.js'
  import createIncentiveModal from './components/createIncentiveModal.vue'
  import infoModal from './components/infoModal.vue'
  import incentiveMgr from '@/biz/bonus/incentive'
  import auditMgr from '@/biz/flowApproval/processAudit.js'
  import extendMgr from '@/biz/bonus/bonusExtend.js'
  import deferModal from '../bill/components/defer-modal.vue'

  export default {
    components: {
      createIncentiveModal,
      infoModal,
      deferModal
    },
    data() {
      return {
        // disFlag:
        //   moment().format('DD') > 25 || moment().format('DD') < 8 ? false : true,
        disFlag: false,
        options: [],
        showEdit: true,
        // 搜索项配置
        searchInfo: {},
        searchOpt: [],
        filterInfo: {},
        columns: [],
        pageData: {
          total: 0,
          current: 1,
          rows: 50,
          pageSizeOpts: [50, 40, 30, 20, 10]
        },
        list: [],
        curItem: {},
        labelWidth: 110,
        needExtend: false,
        applyExtend: false
      }
    },
    created() {},
    mounted() {
      this.columns = columnOpt.options(this, true)
      this.searchOpt = searchOpt.options(this, true)
      this.getList()
      this.checkBonusExtend()
    },
    methods: {
      incentiveWithdraw(val) {
        this.$Modal.confirm({
          title: '撤回',
          content: '<p>确认撤回？</p>',
          onOk: () => {
            let params = {
              auditType: 4,
              businessId: val.headerId
            }
            auditMgr.back(params).then((res) => {
              this.getList()
            })
          },
          onCancel: () => {
            this.$Message.info('你取消了撤回。')
          }
        })
      },
      createIncentive() {
        this.$refs.createIncentive.create()
      },
      success(res) {
        this.$router.push({
          path: `incentive/incentiveLaunch`,
          query: {
            headerId: res
          }
        })
      },
      checkBonusExtend() {
        return extendMgr.checkBonusExtend({ housesId: this.$dt.buildInfo().housesId }).then((res) => {
          this.needExtend = res.needExtend
          this.applyExtend = res.applyExtend
          return Promise.resolve(res)
        })
      },
      onDefer() {
        this.checkBonusExtend().then((res) => {
          if (res.apply) {
            this.$Modal.info({
              title: '通知',
              content: '该项目已提交延期发起申请，请等待审批',
              okText: '关闭'
            })
          } else {
            if (res.needExtend) {
              this.$refs.defer.show({})
            } else {
              this.$Modal.info({
                title: '通知',
                content: '当前可直接发起奖金单，无需申请延期发起',
                okText: '关闭'
              })
            }
          }
        })
      },
      //编辑
      incentiveEdit(val) {
        this.$router.push({
          path: `incentive/incentiveLaunch`,
          query: {
            headerId: val.headerId
          }
        })
      },
      //删除
      incentiveDelete(val) {
        this.$Modal.confirm({
          title: '删除',
          content: '<p>确认删除？</p>',
          onOk: () => {
            let params = {
              headerId: val.headerId
            }
            incentiveMgr.delBonusStimulate(params).then((res) => {
              this.getList()
            })
          },
          onCancel: () => {
            this.$Message.info('你取消了删除。')
          }
        })
      },
      // 查看
      incentiveSee(val) {
        this.$router.push({
          path: `incentive/incentiveSee`,
          query: {
            headerId: val.headerId
          }
        })
      },
      //流程信息
      incentiveProcess(val) {
        let params = {
          businessId: val.headerId,
          auditType: 4
        }
        this.$refs.infoModal.create(params)
      },
      // 重新发起
      incentiveRelaunch(val) {
        this.$Modal.confirm({
          title: '重新发起',
          content: '<p>确认重新发起？</p>',
          onOk: () => {
            let params = {
              headerId: val.headerId
            }
            incentiveMgr.copyBonusStimulate(params).then((res) => {
              // this.getList()
              this.$router.push({
                path: `incentive/incentiveLaunch`,
                query: {
                  headerId: res
                }
              })
            })
          },
          onCancel: () => {
            this.$Message.info('你取消了重新发起。')
          }
        })
      },
      onFilter(data) {
        this.pageData.current = 1
        this.filterInfo = { ...data }
        this.getList()
      },

      getList() {
        this.$refs.table.loading = true
        let params = {
          page: this.pageData.current,
          rows: this.pageData.rows,
          ...this.searchInfo
        }
        incentiveMgr
          .selectBonusStimulateList(params)
          .then((res) => {
            this.list = res.list
            this.pageData.total = Number(res.total)
            this.$refs.table.loading = false
          })
          .catch((err) => {
            this.$refs.table.loading = false
          })
      },
      // 搜索
      onSearch(val) {
        this.pageData.current = 1
        this.searchInfo = { ...val }
        this.getList()
      },
      // 清空筛选条件
      onReset() {
        this.pageData.current = 1
        this.searchInfo = {}
        this.list = []
        this.getList()
      },
      // 分页 页码改变事件
      pageOnChangeFun(current) {
        this.getList()
      },
      // 分页 每页条数改变事件
      pageOnPageSizeChangeFun(rows) {
        this.getList()
      }
    }
  }
</script>

<style lang="scss" scoped>
  .incentive {
    padding: 20px;
  }
</style>
